using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using Unity.VisualScripting;
using UnityEngine;

///<summary>
///
///</summary>

    
public static class NumStrTools
{
    /// <summary>
    /// 获得纯数字
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string GetNumber(string key)
    {
        return System.Text.RegularExpressions.Regex.Replace(key, @"[^\d]*", "");
    }
    /// <summary>
    /// 获得无数字
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string GetNoNumber(string key)
    {
        return System.Text.RegularExpressions.Regex.Replace(key, @"\d", "");
    }
    public static int ASCIIchange(string input)
    {
        if (input.Length > 1) return -1;
        byte[] array = System.Text.Encoding.ASCII.GetBytes(input);
        return (int)(array[0]);
    }
    public static string ASCIIchange(int input)
    {
        System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
        byte[] byteArray = new byte[] { (byte)input };
        return asciiEncoding.GetString(byteArray);

    }
    /// <summary>
    /// 屏幕坐标转换成 UI 坐标
    /// </summary>
    /// <param name="targetParentRect"> 目标 UI 父物体的 RectTransform </param>
    /// <param name="mousePos"> 鼠标位置 </param>
    /// <param name="canvasCam"> 如果Canvas的渲染模式为: Screen Space - Overlay, Camera 设置为 null;
    /// Screen Space-Camera or World Space, Camera 设置为 Camera.main></param>
    /// <returns>UI 的局部坐标</returns>
    public static Vector2 ScreenToUILocalPos(RectTransform targetParentRect, Vector2 mousePos, Camera canvasCam )
    {
        //UI 的局部坐标
        Vector2 uiLocalPos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(targetParentRect, mousePos, canvasCam, out uiLocalPos);
        return uiLocalPos;
    }

    /// <summary>
    /// 将一个字典的键名更改为另一个键名
    /// 如果新键有值，会覆盖新键的值
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    /// <param name="dic"></param>
    /// <param name="oldKey"></param>
    /// <param name="newKey"></param>
    /// <returns></returns>
    public static bool ChangeDictionaryKeyName<TKey, TValue>(this IDictionary<TKey, TValue> dic, TKey oldKey, TKey newKey)
    {
        TValue value;
        if (!dic.TryGetValue(oldKey, out value))
            return false;
        dic.Remove(oldKey);
        dic[newKey] = value;
        return true;
    }

}